package com.losers;

import java.util.concurrent.atomic.AtomicBoolean;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends Activity {

	AtomicBoolean isActive = new AtomicBoolean(true);
	
	BeerAdapter adapter = null;
	Cursor model = null;
	
	SQLiteDatabase db = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // HUOM READABLE!!! ei writeable koska sit� ei nyt tarvi?
        db = (new Database(this)).getWritableDatabase();
        
        ListView list=(ListView)findViewById(R.id.ListViewBeers);
        
        model = Beer.getAll(db);
        startManagingCursor(model);
		adapter = new BeerAdapter(model);
		list.setAdapter(adapter);
		list.setOnItemClickListener(onListClick);
    }
    
    
    private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Intent intent = new Intent(MainActivity.this, BeerForm.class);
 
			intent.putExtra(BeerForm.INTENT_BEER_ID, String.valueOf(id));
			startActivity(intent);
		}
	};
	
	public void onAddNewBeerClicked(View v) {
		Intent intent = new Intent(this, BeerForm.class);
		startActivity(intent);
	}
    
    @Override
	public void onPause() {
		super.onPause();
		
		isActive.set(false);
	}
 
	@Override
	public void onResume() {
		super.onResume();
 
		isActive.set(true);
	}
	
	
	class BeerAdapter extends CursorAdapter
	{
		public BeerAdapter(Cursor c) {
			super(MainActivity.this, c);
		}
		
		@Override
		public void bindView(View row, Context ctxt, Cursor c) {
			
			BeerWrapper wrapper = (BeerWrapper)row.getTag();
			wrapper.populateFrom(c);
		}

		@Override
		public View newView(Context context, Cursor c, ViewGroup parent) {
			
			LayoutInflater inflater = getLayoutInflater();
			View row = inflater.inflate(R.layout.row, parent, false);
			BeerWrapper wrapper=new BeerWrapper(row);
 
			row.setTag(wrapper);
			wrapper.populateFrom(c);
	
			return row;
		}
		
	}
	
	class BeerWrapper {
		private TextView name = null;
		private TextView brewery = null;
		private RatingBar rating = null;
		private View row = null;
 
		BeerWrapper(View row) {
			this.row = row;
		}
 
		void populateFrom(Cursor c) {
			getName().setText(c.getString(c.getColumnIndex("name")));
			getBrewery().setText(c.getString(c.getColumnIndex("brewery")));
			getRating().setRating(c.getFloat(c.getColumnIndex("rating")));
		}
 
		TextView getName() {
			if (name == null)
				name = (TextView)row.findViewById(R.id.TextViewRowName);
			return name;
		}
		
		TextView getBrewery() {
			if (brewery == null)
				brewery = (TextView)row.findViewById(R.id.TextViewRowBrewery);
			return brewery;
		}
		
		RatingBar getRating() {
			if (rating == null)
				rating = (RatingBar)row.findViewById(R.id.RatingBarRatingSmall);
			return rating;
		}
	}
}
